const mysql = require('mysql')
const { DATA_BASE } = require('../config/env')

const connection = mysql.createConnection(DATA_BASE);
console.log('开始链接数据库', connection)
// 开始链接
connection.connect();
function execSQL(sql) {
  const promise = new Promise((resovle, reject) => {
    connection.query(sql, (err, result) => {
      if (err) {
        reject(err);
        return;
      }
      if (result.length < 1) {
        resovle({
          code: -1,
          message: '查询失败'
        })
        return;
      }
      resovle(result)
    })
  })
  return promise;
}

module.exports = execSQL;